﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class orgman_models_addrule : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            MasApp.BLL.Check.CheckLogin();
            if (Request.QueryString["modelname"] != null)
            {
                lb_currentmodel.Text = Convert.ToString(Request.QueryString["modelname"]);
            }
            else
            {
                MasApp.BLL.ResponseHelper.alert("非法链接！", "list.aspx");
            }
        }
        if (ViewState["rowCount"] == null)
        {
            addTable();
            ViewState["rowCount"] = Convert.ToInt16(ViewState["rowCount"]) + 1;
            Label2.Text = Convert.ToString(ViewState["rowCount"]) + "个";
        }
        else
        {
            for (int i = 0; i < Convert.ToInt16(ViewState["rowCount"]); i++)
            {
                addTable();
            }
        }
    }
    protected void addTable()
    {
        TableRow tr;
        TableCell tc;
        TextBox tb;

        tr = new TableRow();

        tc = new TableCell();
        Label lb = new Label();
        lb.Text = Table1.Rows.Count.ToString();
        tc.Controls.Add(lb);
        tr.Cells.Add(tc);
        lb = new Label();
        lb.Text = "*";

        tc = new TableCell();
        tb = new TextBox();
        tb.ID = "tb_rulename" + Table1.Rows.Count.ToString();
        tc.Controls.Add(tb);
        tc.Controls.Add(lb);
        tr.Cells.Add(tc);

        tc = new TableCell();
        tb = new TextBox();
        tb.ID = "tb_description" + Table1.Rows.Count.ToString();
        tc.Controls.Add(tb);
        tr.Cells.Add(tc);

        Table1.Rows.Add(tr);
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        lb.Text = "";
        if (Request["modelid"] == null)
        {
            MasApp.BLL.ResponseHelper.alert("非法操作", "list.aspx");
        }
        else
        {
            bool bo = true;
            for (int i = 1; i < Table1.Rows.Count; i++)
            {
                if (Convert.ToString(Request["tb_rulename" + i.ToString()]) == "")
                {
                    bo = false;
                    Label2.Text = "请填写完整信息！";
                    break;
                }
            }
            bool bo2 = true;
            if (bo)
            {
                MasApp.Model.rule rule = new MasApp.Model.rule();
                rule.Modelid = int.Parse(Request["modelid"].ToString());
                for (int i = 1; i < Table1.Rows.Count; i++)
                {
                    rule.Rulename = Convert.ToString(Request["tb_rulename" + i.ToString()]);
                    rule.Description = Convert.ToString(Request["tb_description" + i.ToString()]);

                    if (!MasApp.BLL.reg.AddRule(rule))
                    {
                        bo2 = false;
                    }
                }
            }
            else
            {
                bo2 = false;
            }
            if (bo2)
            {
                GridView1.DataSourceID = "SqlDataSource1";
                GridView1.DataBind();
                lb.Text = "准则添加成功！";
                for (int j = Convert.ToInt16(ViewState["rowCount"]); j >= 1; j--)
                {
                    Table1.Rows.RemoveAt(j);
                }
                ViewState["rowCount"] = null;
                Page_Load(sender, e);
            }
            else
            {
                GridView1.DataSourceID = "SqlDataSource1";
                GridView1.DataBind();
                lb.Text = "操作失败了~";
            }
        }
    }
    protected void lbt_add_Click(object sender, EventArgs e)
    {
        if (Table1.Rows.Count < 11)
        {
            addTable();

            ViewState["rowCount"] = Convert.ToInt16(ViewState["rowCount"]) + 1;
            Label2.Text = Convert.ToString(ViewState["rowCount"]) + "个";
        }
        else
        {
            Label2.Text = "最多10个";
        }
    }
    protected void lbt_minus_Click(object sender, EventArgs e)
    {
        if (Table1.Rows.Count > 2)
        {
            Table1.Rows.RemoveAt(Table1.Rows.Count - 1);
            ViewState["rowCount"] = Convert.ToInt16(ViewState["rowCount"]) - 1;
            Label2.Text = Convert.ToString(ViewState["rowCount"]) + "个";
        }
        else
        {
            Label2.Text = "最少1个";
        }
    }
}
